package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.util.TimeUtils;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.example.myapplication.bean.UserBean;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

public class Register extends AppCompatActivity {
    EditText mEtUserName,mEtPassWord,mEtRePassWord;
    Button mBtnRegister;
    private static final String TAG = "MainActivity";
    String data;
    private EditText et_name;
    private String name;
    private String username;
    private String password;
    private String repassword;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initData();
        initEvent();
    }

    private void initData() {
        String url = " http://anli-wechat.boruiman.com/freshmenapp/user/regist";
        username = mEtUserName.getText().toString().trim();
        password = mEtPassWord.getText().toString().trim();
        repassword = mEtRePassWord.getText().toString().trim();
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("username","usernameStr");
            jsonObject.put("password","passwordStr");
            jsonObject.put("repassword","repasswordStr");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestQueue requestQueue = Volley.newRequestQueue(Register.this);
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, jsonObject, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                Log.e(TAG,"onResponse:"+jsonObject.optString("msg"));
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Toast.makeText(Register.this,"网络请求失败",Toast.LENGTH_SHORT).show();
            }
        });
        requestQueue.add(jsonObjectRequest);
    }
    private void initEvent() {

        mBtnRegister.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v){
                final String username =mEtUserName.getText().toString().trim();
                String password = mEtPassWord.getText().toString().trim();
                String repassword = mEtRePassWord.getText().toString().trim();
                Log.e(TAG, "onClick: "+ username);
                if (TextUtils.isEmpty(repassword)){//判断是不是为空，是则“不能为空”，
                    Toast.makeText(Register.this, "不能为空", Toast.LENGTH_SHORT).show();

                }else if (TextUtils.isEmpty(password)){
                    Toast.makeText(Register.this, "不能为空", Toast.LENGTH_SHORT).show();

                }else if (TextUtils.isEmpty(username)){
                    Toast.makeText(Register.this, "不能为空", Toast.LENGTH_SHORT).show();

                }else if (password.equals(repassword)){
                    Toast.makeText(Register.this, "注册成功"+username, Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(Register.this,Login.class);//从Register跳转到Login页面
                    startActivity(intent);
                }else {
                    Toast.makeText(Register.this, "密码不相等", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    private void initView() {

        mEtUserName = findViewById(R.id.et_username);
        mEtPassWord = findViewById(R.id.et_password);
        mEtRePassWord = findViewById(R.id.et_repassword);
        mBtnRegister = findViewById(R.id.btn_register);


    }

}